package map
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import map.data.Front;
	import map.data.Mapdata;
	import map.data.RadCell;

	public class BaseMapView extends Sprite
	{
		protected var _background:Bitmap;
		protected var _mapData:Mapdata;
		
		protected var _showCellLayer:Shape;
		protected var _itemLayer:Sprite;
		
		protected var _viewWidth:Number;
		protected var _viewHeight:Number;
		
		public function BaseMapView(viewWidth:Number,viewHeight:Number)
		{
			_showCellLayer = new Shape();
			_itemLayer = new Sprite();
			_viewWidth = viewWidth;
			_viewHeight = viewHeight;
			addChild(_showCellLayer);
			addChild(_itemLayer);
		}
		
		public function viewAt(point:Point,priority:int=1,tweenAble:Boolean=false):void
		{
			this.x = _viewWidth*.5 - point.x;
			this.y = _viewHeight*.5 - point.y;
			if(this.x > 0)
			{
				this.x =0;
			}
			if(this.x < _viewWidth - this.width)
			{
				this.x = _viewWidth - this.width;
			}
			if(this.y > 0)
			{
				this.y =0;
			}
			if(this.y < _viewHeight-this.height)
			{
				this.y = _viewHeight-this.height;
			}
		}
		
		public function addLiving(living:DisplayObject,positionIndex:int=-1):void
		{
			if(positionIndex == -1)
			{
				positionIndex = getIndexByPosition(_mapData.birthpoints[0]);
			}
			var cell:RadCell = _mapData.cells[positionIndex] as RadCell;
			living.x = cell.center.x;
			living.y = cell.center.y;
			addChild(living);
		}
		
		public function showCells(cells:Vector.<RadCell>):void
		{
			_showCellLayer.graphics.clear();
			_showCellLayer.graphics.lineStyle(3,0xffffff);
			for each(var cell:RadCell in cells)
			{
				var p1:Point = Point.interpolate(cell.point1,cell.point4,0.3);
				var p2:Point = Point.interpolate(cell.point2,cell.point3,0.3);
				var p3:Point = Point.interpolate(cell.point3,cell.point2,0.3);
				var p4:Point = Point.interpolate(cell.point4,cell.point1,0.3);
				_showCellLayer.graphics.moveTo(p1.x,p1.y);
				_showCellLayer.graphics.lineTo(p2.x,p2.y);
				_showCellLayer.graphics.lineTo(p4.x,p4.y);
				_showCellLayer.graphics.lineTo(p3.x,p3.y);
				_showCellLayer.graphics.lineTo(p1.x,p1.y);
			}
		}
		
		public function removeLiving(living:DisplayObject):void
		{
			
		}
		
		public function getCellsByIndex(positionIndex:int,mobility:int,unitMobility:int):Vector.<RadCell>
		{
			var cell:RadCell = _mapData.cells[positionIndex];
			var result:Vector.<RadCell> = new Vector.<RadCell>();
			var nextCells:Vector.<RadCell> = new Vector.<RadCell>();
			var tempCells:Vector.<RadCell> = new Vector.<RadCell>();
			nextCells.push(cell);
			while(mobility>=0)
			{
				for each(cell in nextCells)
				{
					cell.surplusMobility = mobility;
					result.push(cell);
				}
				for each(cell in nextCells)
				{
					for each(var neighborIndex:int in cell.neighbors)
					{
						var neighbor:RadCell = _mapData.cells[neighborIndex]
						if(result.indexOf(neighbor)<0 && nextCells.indexOf(neighbor)<0){
							tempCells.push(neighbor);
							neighbor.parent = cell;
						}
					}
				}
				nextCells = tempCells;
				tempCells = new Vector.<RadCell>();
				mobility -= unitMobility;
			}
			tempCells = result;
			result = new Vector.<RadCell>();
			for each(cell in tempCells)
			{
				result.push(cell.clone());
			}
			return result;
		}
		
		public function getCellsByPosition(position:Point,mobility:int,unitMobility:int):Vector.<RadCell>
		{
			var index:int = getIndexByPosition(position);
			return getCellsByIndex(index,mobility,unitMobility);
		}
		
		public function getIndexByPosition(position:Point):int
		{
			var index:int = -1;
			for each(var cell:RadCell in _mapData.cells)
			{
				if(cell.containPoint(position))
				{
					index = _mapData.cells.indexOf(cell);
					break;
				}
			}
			return index;
		}

		public function get viewWidth():Number
		{
			return _viewWidth;
		}

		public function get viewHeight():Number
		{
			return _viewHeight;
		}


	}
}